package com.joysuch.wwyt.screen.service.impl;

import cn.zlg.common.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.joysuch.wwyt.bp.bean.BpAddDepartBean;
import com.joysuch.wwyt.bp.entity.BpDepart;
import com.joysuch.wwyt.bp.service.BpDepartService;
import com.joysuch.wwyt.common.constant.Constant;
import com.joysuch.wwyt.core.enums.Status;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.screen.entity.ScreenMapPerspective;
import com.joysuch.wwyt.screen.mapper.ScreenMapPerspectiveMapper;
import com.joysuch.wwyt.screen.service.ScreenMapPerspectiveService;
import com.joysuch.wwyt.screen.vo.ScreenMapPerspectiveVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jwk
 * @since 2021-09-06
 */
@Service
public class ScreenMapPerspectiveServiceImpl extends ServiceImpl<ScreenMapPerspectiveMapper, ScreenMapPerspective> implements ScreenMapPerspectiveService {

    @Autowired
    private ScreenMapPerspectiveMapper screenMapPerspectiveMapper;
    @Autowired
    private BpDepartService bpDepartService;

    @Override
    public List<ScreenMapPerspectiveVO> findList() {
        List<ScreenMapPerspectiveVO> voList = new ArrayList<>();
        List<ScreenMapPerspective> perspectiveList = findPerspectiveList();
        perspectiveList.forEach(each -> {
            ScreenMapPerspectiveVO mapPerspectiveVO = new ScreenMapPerspectiveVO();
            BeanUtils.copyProperties(each, mapPerspectiveVO);
            List<String> curConfigDpIds = transformStrToList(each.getEffectiveDepartmentIds());
            List<BpAddDepartBean> departList = new ArrayList<>();
            curConfigDpIds.forEach(id -> {
                BpDepart depart = bpDepartService.findById(Long.valueOf(id));
                BpAddDepartBean departBean = new BpAddDepartBean();
                departBean.setName(depart.getName());
                departBean.setId(depart.getId());
                departList.add(departBean);
            });
            mapPerspectiveVO.setDepartList(departList);
            voList.add(mapPerspectiveVO);
        });
        return voList;
    }

    private List<String> transformStrToList(String effectiveDepartmentIds) {
        if (StringUtils.isNotBlank(effectiveDepartmentIds)) {
            return Splitter.on(Constant.SPLIT_COMMA).trimResults().splitToList(effectiveDepartmentIds);
        }
        return Lists.newArrayList();
    }

    private List<ScreenMapPerspective> findPerspectiveList() {
        LambdaQueryWrapper<ScreenMapPerspective> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ScreenMapPerspective::getDeleteFlag, "0");
        wrapper.eq(ScreenMapPerspective::getTenentId, Context.getCurrentTenantId());
        wrapper.likeRight(ScreenMapPerspective::getOrgCode, Context.getCompanyCode());
        return super.list(wrapper);
    }

    @Override
    public ScreenMapPerspective getPerspectiveByCurUser() {
        Long departId = Context.getCurrentUser().getDepartId();
        return screenMapPerspectiveMapper.getPerspectiveByUserId(departId, Context.getCurrentTenantId(), Context.getCompanyCode());
    }

    @Override
    public boolean saveMapPerspective(ScreenMapPerspective screenMapPerspective) throws ApiBusinessException {
        checkData(screenMapPerspective);
        String effectiveDepartmentIds = screenMapPerspective.getEffectiveDepartmentIds();
        screenMapPerspective.setEffectiveDepartmentIds(effectiveDepartmentIds == null ? Constant.EMPTY_STR : effectiveDepartmentIds);
        return super.saveOrUpdate(screenMapPerspective);
    }

    private void checkData(ScreenMapPerspective screenMapPerspective) throws ApiBusinessException {
        List<ScreenMapPerspective> perspectives = findPerspectiveList();
        if (CollectionUtils.isNotEmpty(perspectives)) {
            if (screenMapPerspective.getId() != null) {
                perspectives.removeIf(each -> each.getId().equals(screenMapPerspective.getId()));
            }
            Set<String> perspectiveNames = perspectives.stream().map(ScreenMapPerspective::getPerspectiveName).collect(Collectors.toSet());
            if (perspectiveNames.contains(screenMapPerspective.getPerspectiveName())) {
                throw new ApiBusinessException(Status.BIG_SCREEN_PERSPECTIVE_NAME_EXIST);
            }
            List<String> curConfigDpIds = transformStrToList(screenMapPerspective.getEffectiveDepartmentIds());
            List<String> departmentIds = perspectives.stream().map(ScreenMapPerspective::getEffectiveDepartmentIds).collect(Collectors.toList());
            for (String id : curConfigDpIds) {
                if (CollectionUtils.isNotEmpty(departmentIds)) {
                    for (String each : departmentIds) {
                        if (StringUtils.isNotBlank(each) && each.contains(id)) {
                            throw new ApiBusinessException(Status.BIG_SCREEN_PERSPECTIVE_DEPART_EXIST);
                        }
                    }
                }
            }
        }
    }
}
